/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package lcfgly.spring.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;

/**
 * StringToDate
 * @author lcfgly
 * @Date 2015-1-29 9:31:10
 * @version 1.0
 */
public class StringToDate implements Converter<String, Date>{

    @Override
    public Date convert(String source) {
        if(source != null ){
            SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            SimpleDateFormat ds=new SimpleDateFormat("yyyy-MM-dd");
            try {
                return sdf.parse(source);
            } catch (ParseException ex) {
                try{
                    return ds.parse(source);
                }catch(ParseException e){
                    System.out.println("日期转换错误");
                    return new Date();
                }
            }
        }else{
           return new Date();
        }
    }

}
